/**
 * 	 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *   @author Colin Redmond
 *   Copyright (C) 2011 Colin Redmond
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package com.cred.industries.platform.view.account {
	
	import com.cred.industries.platform.business.TaskEvent;
	import com.cred.industries.platform.business.facade.CustomerFacade;
	import com.cred.industries.platform.model.Config;
	import com.cred.industries.platform.view.account.skin.CustomersSkin;
	
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.events.FlexEvent;
	import mx.states.State;
	
	import spark.components.Button;
	import spark.components.Group;
	import spark.components.List;
	import spark.components.SkinnablePopUpContainer;
	import spark.components.TextInput;
	
	public class Customers extends SkinnablePopUpContainer {
		//movie clips on stage
		[SkinPart(required="true")] public var customerList:List;
		[SkinPart(required="true")] public var personaName:TextInput;
		[SkinPart(required="true")] public var findBtn:Button;
		[SkinPart(required="true")] public var previous:Button;
		[SkinPart(required="true")] public var next:Button;
		
		//
		private var mCustFacade:CustomerFacade = new CustomerFacade();
		
		public function Customers() {
			
			super();
			addEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler, false, 0, true);
			
			if(Config.isMobile()) {
				setStyle("skinClass", com.cred.industries.platform.view.account.skin.CustomersSkin);
			} else {
				setStyle("skinClass", com.cred.industries.platform.view.account.skin.CustomersSkin);
			}
			
			states = [
				new State({name:"normal"}),
				new State({name:"wait"})];
		}
		
		private function creationCompleteHandler(event:FlexEvent):void {
			
			trace("creationCompleteHandler");
			registerListeners();
			
			personaName.setFocus();
		}
		
		private function registerListeners():void {
			
			findBtn.addEventListener(MouseEvent.CLICK, findClicked, false, 0, true);
			previous.addEventListener(MouseEvent.CLICK, previousClicked, false, 0, true);
			next.addEventListener(MouseEvent.CLICK, nextClicked, false, 0, true);
		}
		
		private function findClicked(event:MouseEvent):void {
			
			currentState = "wait";
			mCustFacade.addEventListener(TaskEvent.TASK_COMPLETE, findCustomersComplete, false, 0, true);
			mCustFacade.addEventListener(TaskEvent.TASK_FAILED, taskFailedComplete, false, 0, true);
			mCustFacade.findCustomerByPersonaName(personaName.text);
		}
		
		private function previousClicked(event:MouseEvent):void {
			
			mCustFacade.addEventListener(TaskEvent.TASK_COMPLETE, findCustomersComplete, false, 0, true);
			mCustFacade.addEventListener(TaskEvent.TASK_FAILED, taskFailedComplete, false, 0, true);
			mCustFacade.getPrevious();
		}
		
		private function nextClicked(event:MouseEvent):void {
			
			mCustFacade.addEventListener(TaskEvent.TASK_COMPLETE, findCustomersComplete, false, 0, true);
			mCustFacade.addEventListener(TaskEvent.TASK_FAILED, taskFailedComplete, false, 0, true);
			mCustFacade.getNext();	
		}
		
		private function findCustomersComplete(event:TaskEvent):void {
			
			currentState = "normal";
			customerList.dataProvider = new ArrayCollection(mCustFacade.custFound);
			next.visible = mCustFacade.hasNext();
			previous.visible = mCustFacade.hasPrevious();
		}
		
		private function taskFailedComplete(event:TaskEvent):void {
			
			currentState = "normal";
		}
	}
}